<?php

namespace App\Models\Cruise;

use App\Models\BaseModel;
use App\Models\Kms\User\KmsUser;
use App\Models\Traits\KfUid;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

class CruiseOrderInputModel extends BaseModel
{
    use KfUid;

    protected $table    = 'cruise_order_inputs';
    protected $fillable = [
        'orderId',
        'templateType',
        'sysTemplate',
        'userContentJson',
        'inputObjectJson',
    ];

    public function getUserContentJsonArrAttribute(): array
    {
        return json_decode($this->userContentJson ?? '[]', true);
    }

    public function getInputObjectJsonArrAttribute(): array
    {
        return json_decode($this->inputObjectJson ?? '[]', true);
    }

    public static function getData(int $sysTemplate, int $orderId)
    {
        return self::query()->where('orderId', $orderId)->where('sysTemplate', $sysTemplate)->first();
    }

    /**
     * @return HasOne
     */
    public function updatedByUserRel(): HasOne
    {
        return $this->hasOne(KmsUser::class, "rmsUserId", "updatedByUid");
    }

    public function inputTypesRel(): HasMany
    {
        return $this->hasMany(CruiseOrderInputTypeModel::class, 'orderId', 'orderId');
    }
}
